package com.wxd.framework.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sun.mail.util.LogOutputStream;
import com.wxd.framework.common.FrameConstants;
import com.wxd.framework.common.web.RequestUtils;
import com.wxd.framework.entity.Menu;
import com.wxd.framework.entity.User;
import com.wxd.framework.service.FrameService;
import com.wxd.framework.vo.TreeItem;

@Controller
@RequestMapping("/")
public class FrameController extends BaseController {

	@Autowired
	private FrameService frameService;
	
	/**
	 * TO:登录页面
	 * @return
	 */
	@RequestMapping(value = "/login.do", method = RequestMethod.GET)
	public String login(){
		return "frame/login";
	}
	
	/**
	 * TO:主页面
	 * @return
	 */
	@RequestMapping("/main.do")
	public String main(HttpServletRequest request,ModelMap model){
		model.addAttribute("userName", super.getCurrUser(request).getRealName());
		return "frame/mainFrame";
	}
		
	/**
	 * TO:欢迎页面
	 * @return
	 */
	@RequestMapping("/welcome.do")
	public String welcome(HttpServletRequest request, ModelMap model){
		User user = super.getCurrUser(request);
		model.addAttribute("user",user);
		return "frame/welcome";
	}
	
	/**
	 * 登录验证
	 * @param userName
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public String login(String userName, String password,HttpServletRequest request, HttpServletResponse response, ModelMap model){
		User user = frameService.loginValidate(userName, password);
		if (StringUtils.isNotBlank(user.getMessage())) {
			model.addAttribute("loginerr", user.getMessage());
		}else{
			super.session.setAttributeObject(request, response, FrameConstants.SESSON_KEY_CURRUSER, user);
			super.session.setAttribute(request, response, FrameConstants.SESSON_KEY_CURRUSERID, user.getId());
			return "redirect:main.do";
		}
		return "frame/login";
	}
	
	/**
	 * 注销当前用户
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/logout.do")
	public void logout(HttpServletRequest request, HttpServletResponse response){
		String id = (String)super.session.getAttribute(request, FrameConstants.SESSON_KEY_CURRUSERID);
		if (StringUtils.isNotBlank(id)) {
			super.session.logout(request, response);
		}
		super.renderJsonExcuteSuccess(response);
	}
	
	@RequestMapping("/resetPassword.do")
	public void resetPassword(String password, HttpServletRequest request, HttpServletResponse response){
		String userId = super.getCurrUser(request).getId();
		if (StringUtils.isNotBlank(password)) {
			frameService.resetPassword(userId, password);
			super.renderJsonExcuteSuccess(response);
		}else {
			super.renderJsonExcuteFail("密码不能为空", response);
		}
	}
	
	/**
	 * 获取树形导航菜单列表
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getMenuTreeItems.do")
	public void getMenuTreeItems(HttpServletRequest request, HttpServletResponse response){
		String parentId = RequestUtils.getQueryParam(request, "id");
		String userId = super.getCurrUser(request).getId();
		List<TreeItem> list = frameService.getSysMenuItemsByParentId(parentId, userId);
		super.renderList(list, response);
	}

}
